//  阴阳性选择按钮
//  YZEUIButtonChoose.m
//  yilutongxing
//
//  Created by T&J on 2017/6/9.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "YZEUI_ToggleButton.h"

#define BUTTON_COLOR_NORMAL [UIColor colorWithRed:60/255.0 green:60/255.0 blue:60/255.0 alpha:1.0]

@interface YZEUI_ToggleButton()
@property (strong, nonatomic) UILabel *normalabel;
@property (strong, nonatomic) UILabel *selectedlabel;
@property (strong, nonatomic) NSString *normaltitle;
@property (strong, nonatomic) NSString *selectedtitle;
@end

@implementation YZEUI_ToggleButton

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self didInitialize];
    }
    return self;
}

- (instancetype)initWithTitle:(NSString *)normaltitle selectedtitle:(NSString *)selectedtitle{
    if (self = [super init]) {
        CGRect rect = CGRectMake(0, 0, 120, 30);
        self.frame = rect;
        self.normaltitle = normaltitle;
        self.selectedtitle = selectedtitle;
    }
    [self didInitialize];
    return self;
}

- (void)didInitialize{
    self.selected = NO;
    [self setBackgroundImage:[UIImage imageNamed:DEFAULT_BG_NORMAL] forState:UIControlStateNormal];
    [self setTitleColor:BUTTON_COLOR_NORMAL forState:UIControlStateNormal];
    [self.titleLabel setFont:[UIFont systemFontOfSize:15]];
    self.normalabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.width/2, self.height)];
    _normalabel.textColor = WHITE_COLOR;
    _normalabel.textAlignment = NSTextAlignmentCenter;
    _normalabel.text = _normaltitle;
    self.selectedlabel = [[UILabel alloc]initWithFrame:CGRectMake(self.normalabel.width, 0, self.normalabel.width, self.normalabel.height)];
    _selectedlabel.textColor = Bluecolor;
    _selectedlabel.textAlignment = NSTextAlignmentCenter;
    _selectedlabel.text = _selectedtitle;
    [self addSubview:_normalabel];
    [self addSubview:_selectedlabel];
    
    //自我响应点击事件
    [self addTarget:self action:@selector(OnClicked:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setSelected:(BOOL)selected{
    if (selected) {
        [self setBackgroundImage:[UIImage imageNamed:DEFAULT_BG_SELECTED] forState:UIControlStateSelected];
        [self setTitleColor:WHITE_COLOR forState:UIControlStateSelected];
        _normalabel.textColor = Bluecolor;
        _selectedlabel.textColor = WHITE_COLOR;
    }
    else {
        [self setBackgroundImage:[UIImage imageNamed:DEFAULT_BG_NORMAL] forState:UIControlStateNormal];
        [self setTitleColor:BUTTON_COLOR_NORMAL forState:UIControlStateNormal];
        _normalabel.textColor = WHITE_COLOR;
        _selectedlabel.textColor = Bluecolor;
    }
    super.selected = selected;
}

- (BOOL)getSelected{
    return super.selected;
}

- (void)setHighlighted:(BOOL)highlighted{
    //阻止长按时变灰效果，故意留空
}

-(void)OnClicked:(YZEUI_ToggleButton*)sender{
    sender.selected = !sender.selected;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
